package com.cn.jinl.service;

import com.cn.jinl.api.ITerminalAccessService;
import com.cn.jinl.dao.TerminalAccessMapper;
import com.cn.jinl.domain.TerminalAccess;
import com.cn.jinl.domain.TerminalAccessExample;
import com.cn.jinl.utils.CollectionsUtil;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created On : 2024/1/10.
 * <p>
 * Author : Huiyan
 * <p>
 * Description: TerminalAccessServiceImpl
 */
@Service
public class TerminalAccessServiceImpl implements ITerminalAccessService {

    @Autowired
    private TerminalAccessMapper terminalAccessMapper;

    private String mac_address_pattern = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";

    //queried by dept_id || name(means terminal_name)
    @Override
    public List<TerminalAccess> getTerminalList(QueryForm queryForm) {
        List<TerminalAccess> list = terminalAccessMapper.selectTerminalList(queryForm);
        return list;
    }

    @Override
    public String addTerminalList(List<TerminalAccess> terminalAccesses) {

        if (CollectionsUtil.isEmpty(terminalAccesses)){
            return "请添加终端数据";
        }

        List<String> macAddressList = Lists.newArrayList();
        for (TerminalAccess item : terminalAccesses){
            String result = this.terminalInfoIsLegal(item);
            if (!"".equals(result)){
                return result;
            }

            String id = CommonUtil.generateRandomNum("terminal_");
            item.setId(id);
            item.setStatus(Constants.NORMAL_STATUS);

            macAddressList.add(item.getMacAddress());
        }

        TerminalAccessExample example = new TerminalAccessExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andMacAddressIn(macAddressList);
        int existsCount = terminalAccessMapper.countByExample(example);
        if (existsCount > 0){
            return "mac地址已存在";
        }

        int count = terminalAccessMapper.batchInsertTerminal(terminalAccesses);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateTerminal(TerminalAccess terminalAccess) {

        String id = terminalAccess.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.terminalInfoIsLegal(terminalAccess);
        if (!"".equals(result)){
            return result;
        }

        if (this.countMacAddress(terminalAccess.getMacAddress(),id) > 0){
            return "设备Mac地址已存在";
        }

        TerminalAccessExample example = new TerminalAccessExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);

        int count = terminalAccessMapper.updateByExampleSelective(terminalAccess,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteTerminalInfo(QueryForm queryForm) {

        List<String> ids = queryForm.getIds();
        if (CollectionsUtil.isEmpty(ids)){
            return "delete terminal id is null";
        }

        TerminalAccess terminalAccess = new TerminalAccess();
        terminalAccess.setStatus(Constants.DEL_STATUS);
        TerminalAccessExample example = new TerminalAccessExample();
        example.createCriteria().andIdIn(ids);
        int count = terminalAccessMapper.updateByExampleSelective(terminalAccess,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, Object> getTerminalAllInfo(QueryForm queryForm) {
        Map<String,Object> map = Maps.newHashMap();
        int totalCount = terminalAccessMapper.countTotal();
        int accessibleCount = terminalAccessMapper.countAccessible();
        int unaccessibleCount = totalCount - accessibleCount;

        map.put("totalCount",totalCount);
        map.put("accessibleCount",accessibleCount);
        map.put("unaccessibleCount",unaccessibleCount);
        map.put("list",this.getTerminalList(queryForm));

        return map;
    }


    @Override
    public List<TerminalAccess>  getMac(String macAddress) {
        List<TerminalAccess> list= terminalAccessMapper.getMacList(macAddress);
        return list;
    }



    private int countMacAddress(String macAddress,String id){
        TerminalAccessExample example = new TerminalAccessExample();
        TerminalAccessExample.Criteria criteria = example.createCriteria();

        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andMacAddressEqualTo(macAddress);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = terminalAccessMapper.countByExample(example);
        return count;
    }


    private String terminalInfoIsLegal(TerminalAccess terminalAccess){

        String result = "";

        if (StringUtils.isBlank(terminalAccess.getDeptId())){
            result = "请选择使用部门";
            return result;
        }

        if (StringUtils.isBlank(terminalAccess.getTerminalName())){
            result = "请输入终端名称";
            return result;
        }

        if (StringUtils.isBlank(terminalAccess.getMacAddress())){
            result = "请输入Mac地址";
            return result;
        }

        if (StringUtils.isBlank(terminalAccess.getAccessAuthorization())){
            result = "请选择访问授权";
            return result;
        }

        Pattern pattern = Pattern.compile(mac_address_pattern);
        result = pattern.matcher(terminalAccess.getMacAddress()).matches()?"":"请输入正确的MAC地址";
        return result;

    }





}
